<?php
$host="mysql-user.cse.msu.edu"; // Host name
$username="hewittry"; // Mysql username
$password="A39777266"; // Mysql password
$db_name="hewittry"; // Database name
$tbl_name="AllUsers"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['pword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE LogonID='$myusername' and UserPassword='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"

$userType = "Type";

$sqlDealerUser = "SELECT * FROM Dealer WHERE NonAdmins_AllUsers_LogonID='$myusername'";
$sqlRegUser = "SELECT * FROM RegisteredUser WHERE NonAdmins_AllUsers_LogonID='$myusername'";
$sqlAdminUser = "SELECT * FROM Admin WHERE AllUsers_LogonID='$myusername'";
if(mysql_num_rows(mysql_query($sqlDealerUser)) == 1)
{
	$userType = "Dealer";
}
else if(mysql_num_rows(mysql_query($sqlRegUser)) == 1)
{
	$userType = "RegUser";
}
else if(mysql_num_rows(mysql_query($sqlAdminUser)) == 1)
{
	$userType = "Admin";
}
session_start();
$_SESSION["username"] = $myusername;
$_SESSION["pword"] = $mypassword;
$_SESSION["usertype"] = $userType;
header("location:index.php");
}
else {
echo 'Wrong Username or Password, </br><a class="headerlink" href="signin.php">Retry</a>';
}
?>
